﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElingOA6_Admin_DSJCJ.Common
{
    public class OperateLogHelper
    {
        /// <summary>
        /// 增加功能性（页面）日志
        /// </summary>
        /// <param name="perateLogModel"></param>
        public static void AddFunctionOperateLog(string operateContent, string param, string note)
        {
            BLL.OperateLog_MVC operateLogBLL = new BLL.OperateLog_MVC();
            Model.OperateLog_MVC operateLogModel = new Model.OperateLog_MVC();
            operateLogModel.FOperateContent = operateContent;
            operateLogModel.FOperateLevel = "F";
            operateLogModel.FParam = param;
            operateLogModel.FNote = note;
            SetOperationModelValue(ref operateLogModel);
            operateLogBLL.AddOperateLog(operateLogModel);
        }
        /// <summary>
        /// 增加记录(增，删，改等操作）日志
        /// </summary>
        /// <param name="operateProp">表名+关键字段名</param>
        /// <param name="operatePropValue">关键字段值</param>
        public static void AddRecordOperateLog(string operateContent, string operateProp, string operatePropValue, string note)
        {
            BLL.OperateLog_MVC operateLogBLL = new BLL.OperateLog_MVC();
            Model.OperateLog_MVC operateLogModel = new Model.OperateLog_MVC();
            operateLogModel.FOperateContent = operateContent;
            operateLogModel.FOperateLevel = "R";
            operateLogModel.FNote = note;
            operateLogModel.FOperateProp = operateProp;
            operateLogModel.FOperatePropValue = operatePropValue;
            SetOperationModelValue(ref operateLogModel);
            operateLogBLL.AddOperateLog(operateLogModel);
        }
        /// <summary>
        /// 设置日志实体类的值
        /// </summary>
        /// <param name="operateLogModel"></param>
        private static void SetOperationModelValue(ref Model.OperateLog_MVC operateLogModel)
        {
            string visitAddress = HttpContext.Current.Request.Url.AbsoluteUri;
            operateLogModel.FVisitAddress = visitAddress.Split(new char[1] { '?' })[0];
            operateLogModel.FLogID = OperateSession.get(GlobalConstant.PARAM_LOGINID);
            operateLogModel.FLogName = OperateSession.get(GlobalConstant.PARAM_USERNAME);
            operateLogModel.FStartTime = DateTime.Now;
            operateLogModel.FOverTime = operateLogModel.FStartTime;
            operateLogModel.FHost = System.Net.Dns.GetHostName();

            operateLogModel.FCip = GenerateUtil.GetClientIPv4Address();

            operateLogModel.FMethod = HttpContext.Current.Request.RequestType;
            operateLogModel.FUserAgent = HttpContext.Current.Request.UserAgent;
            operateLogModel.FSessionID = HttpContext.Current.Session.SessionID;
            operateLogModel.FBrowser = HttpContext.Current.Request.Browser.Browser + HttpContext.Current.Request.Browser.Version;
            operateLogModel.FOS = HttpContext.Current.Request.Browser.Platform;
        }
    }
}